#include "Oled_Ui.h"


/**
 * @brief 灯光控制菜单（KEY2切换，KEY1确认）
 * @note 最后一项为返回
 */
static void Light_Control_UI(void)
{
    const int COLOR_COUNT = 8;
    const char* color_texts[COLOR_COUNT] = {
        "红色", "绿色", "蓝色",
        "紫色", "黄色", "青色",
        "白色", "返回"
    };

    // 为确保顺序一致，这里手动对应 color 值
    const uint8_t color_values[COLOR_COUNT] = {
        LED_COLOR_RED,
        LED_COLOR_GREEN,
        LED_COLOR_BLUE,
        LED_COLOR_PURPLE,
        LED_COLOR_YELLOW,
        LED_COLOR_CYAN,
        LED_COLOR_WHITE,
        LED_COLOR_NONE // 返回
    };

    int current = 0;     // 当前选中项
    int top_item = 0;    // 当前顶部菜单项

    // 初始显示
    OLED_Clear();
    for (int i = 0; i < 4 && i < COLOR_COUNT; i++) {
        OLED_ShowText(0, i * 16, color_texts[i], OLED_8X16);
    }
    reverse_menu_item(0, 0, 64, 16);
    OLED_Update();

    while (1) {
        if (KEY2 == 0) { // 切换
            while (KEY2 == 0); // 等待释放

            // 清除当前反显
            reverse_menu_item(0, (current - top_item) * 16, 64, 16);

            // 更新索引
            current = (current + 1) % COLOR_COUNT;

            // 滚动处理
            if (current >= top_item + 4) {
                top_item = current - 3;
                OLED_Clear();
                for (int i = 0; i < 4 && (top_item + i) < COLOR_COUNT; i++) {
                    OLED_ShowText(0, i * 16, color_texts[top_item + i], OLED_8X16);
                }
            } else if (current < top_item) {
                top_item = current;
                OLED_Clear();
                for (int i = 0; i < 4 && (top_item + i) < COLOR_COUNT; i++) {
                    OLED_ShowText(0, i * 16, color_texts[top_item + i], OLED_8X16);
                }
            }

            // 新反显
            reverse_menu_item(0, (current - top_item) * 16, 64, 16);
            OLED_Update();
        }
        else if (KEY1 == 0) { // 确认
            while (KEY1 == 0); // 等待释放

            if (current == COLOR_COUNT - 1) {
                // 选中“返回”，退出
                RGB_LED(LED_COLOR_NONE); // 关闭LED
                return;
            }

            // 点亮对应颜色
            RGB_LED(color_values[current]);
        }
    }
}


/**
 * @brief 蜂鸣器控制界面（KEY2切换，KEY1确认）
 * @note 可以设置蜂鸣器开启时间（单位：秒）
 */
static void Buzzer_UI(void)
{
    const int MENU_ITEMS = 4;
    const char* menu_texts[MENU_ITEMS] = {
        "蜂鸣器开", 
        "蜂鸣器关", 
        "设置时间", 
        "返回"
    };

    int current = 0;          // 当前选中项
    int top_item = 0;         // 滚动顶项
    int beep_duration_sec = 1; // 默认蜂鸣器开启 1 秒

    // 初始显示
    OLED_Clear();
    for (int i = 0; i < MENU_ITEMS && i < 4; i++) {
        OLED_ShowText(0, i * 16, menu_texts[i], OLED_8X16);
    }
    reverse_menu_item(0, 0, 64, 16);
    OLED_Update();

    while (1) {
        if (KEY2 == 0) { // 移动选项
            while (KEY2 == 0); // 等待释放

            reverse_menu_item(0, (current - top_item) * 16, 64, 16);
            current = (current + 1) % MENU_ITEMS;

            // 滚动处理
            if (current >= top_item + 4) {
                top_item = current - 3;
                OLED_Clear();
                for (int i = 0; i < 4 && (top_item + i) < MENU_ITEMS; i++) {
                    OLED_ShowText(0, i * 16, menu_texts[top_item + i], OLED_8X16);
                }
            } else if (current < top_item) {
                top_item = current;
                OLED_Clear();
                for (int i = 0; i < 4 && (top_item + i) < MENU_ITEMS; i++) {
                    OLED_ShowText(0, i * 16, menu_texts[top_item + i], OLED_8X16);
                }
            }

            reverse_menu_item(0, (current - top_item) * 16, 64, 16);
            OLED_Update();
        }

        else if (KEY1 == 0) { // 确认选项
            while (KEY1 == 0); // 等待释放

            switch (current) {
                case 0: // 蜂鸣器开
                    BEEP(1);
                    delay_cycles(beep_duration_sec * 32000000); // 秒转时钟周期
                    BEEP(0);
                    break;

                case 1: // 蜂鸣器关
                    BEEP(0);
                    break;

                case 2: { // 设置时间
                    int setting = beep_duration_sec;
                    OLED_Clear();
                    OLED_ShowText(0, 0, "设置时间:", OLED_8X16);
                    OLED_Update();

                    while (1) {
                        // 显示 "当前: " 和数值 + " 秒"
                        OLED_ShowText(0, 16, "当前: ", OLED_8X16); // 或者用"当前"（看字库支持）
                        OLED_ShowNumber(6 * 8, 16, setting, OLED_DEC_UNSIGNED, 1, OLED_8X16); // 6*8=48像素位置显示数字
                        OLED_ShowText(6 * 8 + 8, 16, " 秒", OLED_8X16); // 紧跟数字显示 " 秒"
                        OLED_Update();

                        if (KEY2 == 0) {
                            while (KEY2 == 0);
                            setting++;
                            if (setting > 5) setting = 1;
                        } else if (KEY1 == 0) {
                            while (KEY1 == 0);
                            beep_duration_sec = setting;
                            break;
                        }
                    }

                    // 返回主菜单显示
                    OLED_Clear();
                    for (int i = 0; i < MENU_ITEMS && i < 4; i++) {
                        OLED_ShowText(0, i * 16, menu_texts[i], OLED_8X16);
                    }
                    reverse_menu_item(0, (current - top_item) * 16, 64, 16);
                    OLED_Update();
                    break;
                    }


                case 3: // 返回
                    return;
            }

            // 操作后重新刷新界面
            OLED_Clear();
            for (int i = 0; i < MENU_ITEMS && i < 4; i++) {
                OLED_ShowText(0, i * 16, menu_texts[i], OLED_8X16);
            }
            reverse_menu_item(0, (current - top_item) * 16, 64, 16);
            OLED_Update();
        }
    }
}






/* ========== 一级菜单函数 ========== */

/**
 * @brief 主菜单界面
 * @return 选择的菜单项编号
 */

static int OLED_UI(void) {
    const int MENU_ITEMS = 4;
    const int menu_items[][4] = {
        {0, 0, 64, 16},   // 樂創开源
        {0, 16, 64, 16},  // 接着功能
        {0, 32, 64, 16},  // 设置
        {0, 48, 32, 16}   // 返回
    };
    
    int current = 1; // 当前选中项
    
    OLED_Clear();
    OLED_ShowText(0, 0, "樂創开源",OLED_8X16);
    OLED_ShowText(0, 16, "功能菜单",OLED_8X16);
    OLED_ShowText(0, 32, "设置",OLED_8X16);
    OLED_ShowText(0, 48, "返回",OLED_8X16);
    reverse_menu_item(menu_items[0][0], menu_items[0][1], 
                     menu_items[0][2], menu_items[0][3]);
    OLED_Update();

    while(1) {
        if(!KEY2) {
            reverse_menu_item(menu_items[current-1][0], 
                             menu_items[current-1][1], 
                             menu_items[current-1][2], 
                             menu_items[current-1][3]);
            
            current = (current % MENU_ITEMS) + 1;
            
            reverse_menu_item(menu_items[current-1][0], 
                             menu_items[current-1][1], 
                             menu_items[current-1][2], 
                             menu_items[current-1][3]);
            OLED_Update();
        }
        
        if(!KEY1) {
            OLED_Clear();
            OLED_Update();
            return current;
        }
    }
}

/* ========== 二级菜单函数 ========== */

static int  Lechuang_UI(void)
{
    return 1 ;
}

/**
 * @brief 功能选择菜单
 * @return 0-返回主菜单（本函数实际不返回）
 */
static int FunctionSelect_UI(void) 
{
    const int MENU_ITEMS = 6; // 功能数量
    const char* menu_texts[] = {
        "模式", "灯光", "蓝牙", 
        "蜂鸣器", "陀螺仪", "电池电量"
    };
    
    const int menu_items[][4] = {
        {0, 0, 64, 16},   // 模式
        {0, 16, 64, 16},  // 灯光
        {0, 32, 64, 16},  // 蓝牙
        {0, 48, 64, 16},  // 蜂鸣器
        {0, 0, 64, 16},   // 陀螺仪 (滚动后位置)
        {0, 16, 64, 16}    // 电池电量 (滚动后位置)
    };
    
    int current = 0;    // 当前选中项索引
    int top_item = 0;   // 当前显示的第一个菜单项索引

    // 初始化显示
    OLED_Clear();
    for (int i = 0; i < 4 && i < MENU_ITEMS; i++) {
        OLED_ShowText(0, i * 16, menu_texts[i], OLED_8X16);
    }
    reverse_menu_item(menu_items[0][0], menu_items[0][1], 
                     menu_items[0][2], menu_items[0][3]);
    OLED_Update();

    while(1) {
        // 检查按键
        if(KEY2 == 0) { // 向下切换
            while(KEY2 == 0); // 等待释放
            
            // 清除当前反显
            reverse_menu_item(menu_items[current][0], 
                             menu_items[current][1], 
                             menu_items[current][2], 
                             menu_items[current][3]);
            
            // 移动到下一项
            current = (current + 1) % MENU_ITEMS;
            
            // 检查是否需要滚动
            if (current >= top_item + 4) {
                top_item = current - 3;
                OLED_Clear();
                for (int i = 0; i < 4; i++) {
                    int item_idx = top_item + i;
                    if (item_idx < MENU_ITEMS) {
                        OLED_ShowText(0, i * 16, menu_texts[item_idx], OLED_8X16);
                    }
                }
            }
            else if (current < top_item) {
                top_item = current;
                OLED_Clear();
                for (int i = 0; i < 4; i++) {
                    int item_idx = top_item + i;
                    if (item_idx < MENU_ITEMS) {
                        OLED_ShowText(0, i * 16, menu_texts[item_idx], OLED_8X16);
                    }
                }
            }
            
            // 应用新的反显区域
            int y_pos = (current - top_item) * 16;
            reverse_menu_item(0, y_pos, 64, 16);
            OLED_Update();
        }
        else if(KEY1 == 0) { // 确认选择
            while(KEY1 == 0); // 等待释放
            
            // 根据选择执行不同功能
            switch(current) {
                case 0: // 模式
                    //ModeSelect_UI();
                    break;
                case 1: // 灯光
                    Light_Control_UI();
                    break;
                case 2: // 蓝牙
                    //Bluetooth_UI();
                    break;
                case 3: // 蜂鸣器
                    Buzzer_UI();
                    break;
                case 4: // 陀螺仪
                    //Gyro_UI();
                    break;
                case 5: // 电池电量
                    //Battery_UI();
                    break;
            }
            // 返回后重新显示菜单
            OLED_Clear();
            for (int i = 0; i < 4; i++) {
                int item_idx = top_item + i;
                if (item_idx < MENU_ITEMS) {
                    OLED_ShowText(0, i * 16, menu_texts[item_idx], OLED_8X16);
                }
            }
            reverse_menu_item(0, (current - top_item) * 16, 64, 16);
            OLED_Update();
        }
    }
    
    return 0; // 实际不会执行到此
}

/**
 * @brief 设置菜单
 * @return 0-返回主菜单，其他-设置项编号
 */
static int Settings_UI(void) 
{
    const int MENU_ITEMS = 4; // 设置项数量+返回
    const char* menu_texts[] = {
        "亮度调节", 
        "时间设置", 
        "恢复默认", 
        "返回"
    };
    
    const int menu_items[][4] = {
        {0, 0, 64, 16},   // 亮度调节
        {0, 16, 64, 16},  // 时间设置
        {0, 32, 64, 16},  // 恢复默认
        {0, 48, 32, 16}   // 返回
    };
    
    int current = 0;    // 当前选中项索引
    int returnValue = 0; // 返回值

    // 显示设置菜单
    OLED_Clear();
    for (int i = 0; i < 4 && i < MENU_ITEMS; i++) {
        OLED_ShowText(0, i * 16, menu_texts[i], OLED_8X16);
    }
    reverse_menu_item(menu_items[0][0], menu_items[0][1], 
                     menu_items[0][2], menu_items[0][3]);
    OLED_Update();

    while(1) {
        // 检查按键
        if(KEY2 == 0) { // 向下切换
            while(KEY2 == 0); // 等待释放
            
            // 清除当前反显
            reverse_menu_item(menu_items[current][0], 
                             menu_items[current][1], 
                             menu_items[current][2], 
                             menu_items[current][3]);
            
            // 移动到下一项
            current = (current + 1) % MENU_ITEMS;
            
            // 应用新的反显区域
            reverse_menu_item(menu_items[current][0], 
                             menu_items[current][1], 
                             menu_items[current][2], 
                             menu_items[current][3]);
            OLED_Update();
        }
        else if(KEY1 == 0) { // 确认选择
            while(KEY1 == 0); // 等待释放
            if(current == 3) { // 返回项
                return 0;
            }
            
            // 执行选中的设置项
            OLED_Clear();
            OLED_ShowText(0, 0, "设置项", OLED_8X16);
            OLED_ShowNumber(0, 16, current+1,OLED_DEC_SIGNED, 1, OLED_8X16);
            OLED_Update();
            
            // 这里可以添加设置项执行代码
            // 例如：if(current == 0) BrightnessAdjust();
            
            // 设置执行完成后返回设置值
            returnValue = current + 1;
            
            // 重新显示菜单
            OLED_Clear();
            for (int i = 0; i < MENU_ITEMS; i++) {
                OLED_ShowText(0, i * 16, menu_texts[i], OLED_8X16);
            }
            reverse_menu_item(menu_items[current][0], 
                             menu_items[current][1], 
                             menu_items[current][2], 
                             menu_items[current][3]);
            OLED_Update();
            
            return returnValue;
        }
    }
}

/* ========== 三级菜单函数 ========== */

/**
 * @brief 模式选择菜单
 * @return 0-返回主菜单（本函数实际不返回）
 */
/*static int ModeSelect_UI(void) 
{
    const int MENU_ITEMS = 4; // 模式数量
    const int menu_items[][4] = {
        {0, 0, 64, 16},   // 模式1
        {0, 16, 64, 16},  // 模式2
        {0, 32, 64, 16},  // 模式3
        {0, 48, 64, 16}   // 模式4
    };
    
    int current = 1; // 当前选中项
    
    // 显示模式选择菜单
    OLED_Clear();
    OLED_ShowText(0, 0, "模式一", OLED_8X16);
    OLED_ShowText(0, 16, "模式二", OLED_8X16);
    OLED_ShowText(0, 32, "模式三", OLED_8X16);
    OLED_ShowText(0, 48, "模式四", OLED_8X16);
    reverse_menu_item(menu_items[0][0], menu_items[0][1], 
                     menu_items[0][2], menu_items[0][3]);
    OLED_Update();

    while(1) {
        if(!KEY2) {
            // 切换模式
            // 清除当前反显
            reverse_menu_item(menu_items[current-1][0], 
                                menu_items[current-1][1], 
                                menu_items[current-1][2], 
                                menu_items[current-1][3]);
            
            // 循环切换模式
            current = (current % MENU_ITEMS) + 1;
            
            // 应用新的反显区域
            reverse_menu_item(menu_items[current-1][0], 
                                menu_items[current-1][1], 
                                menu_items[current-1][2], 
                                menu_items[current-1][3]);
            OLED_Update();
            break;
                
        if(!KEY1) { // 确认选择
            // 进入模式运行界面
            // 从模式运行返回后，重新显示菜单
            OLED_Clear();
            OLED_ShowText(0, 0, "模式一", OLED_8X16);
            OLED_ShowText(0, 16, "模式二", OLED_8X16);
            OLED_ShowText(0, 32, "模式三", OLED_8X16);
            OLED_ShowText(0, 48, "模式四", OLED_8X16);
            reverse_menu_item(menu_items[current-1][0], 
                                menu_items[current-1][1], 
                                menu_items[current-1][2], 
                                menu_items[current-1][3]);
            OLED_Update();
            break;
            }
        }
    }
    
    return 0; // 实际不会执行到此
}*/



/* ========== 核心导航函数 ========== */

/**
 * @brief 菜单导航主循环
 */
void Menu_Navigation(void) 
{
    while(1) {
        switch(OLED_UI()) {
            case 1: while(Lechuang_UI() != 0); break;
            case 2: while(FunctionSelect_UI() != 0); break;
            case 3: while(Settings_UI() != 0); break;
            case 4: break;
        }
    }
}


/**
 * @brief 区域反显辅助函数
 * @param x,y 起始坐标
 * @param width,height 区域尺寸
 */
void reverse_menu_item(int x, int y, int width, int height) 
{
    OLED_ReverseArea(x, y, width, height);
}